package org.cupk2.auth.controller;

import jakarta.validation.Valid;
import org.cupk2.auth.domain.Post;
import org.cupk2.auth.dto.PostDto;
import org.cupk2.auth.service.PostServiceInter;
import org.cupk2.tools.bean.CupkBeanUtils;
import org.cupk2.tools.db.DataQuery;
import org.cupk2.tools.db.DataQueryDto;
import org.cupk2.tools.db.DataResult;
import org.cupk2.tools.db.commons.DBSort;
import org.cupk2.tools.db.commons.DBRule;
import org.cupk2.tools.web.CupkResponseCode;
import org.cupk2.tools.web.CupkResponseResult;
import org.cupk2.tools.web.dto.IdAndIdsDto;
import org.cupk2.tools.web.dto.IdDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**角色
 */
@RestController
@RequestMapping("/api/post")
public class PostController {
    @Autowired
    private PostServiceInter postServiceImpl;

    /**
     * 条件查询
     *
     * @param query
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.query')")
    @PostMapping("/query")
    public CupkResponseResult queryAll(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.addDefaultSort(new DBSort("ETIME", DBSort.SORT_TYPE.DESC));
        dataQuery.setRuleAsSql("default", "and (NAME like '%?%' or KEYID like '%?%')");
        DataResult result = postServiceImpl.searchPost(dataQuery);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result);
    }

    /**
     * 获得可用角色
     *
     * @param query
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.ables')")
    @PostMapping("/ables")
    public CupkResponseResult queryAbles(@RequestBody DataQueryDto query) {
        DataQuery dataQuery = DataQuery.getInstance(query);
        dataQuery.setPageSizeAll();
        dataQuery.addDefaultSort(new DBSort("ETIME", DBSort.SORT_TYPE.DESC));
        dataQuery.addRule(new DBRule("A.STATE", "1", "="));
        DataResult result = postServiceImpl.searchPost(dataQuery);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, result.getData());
    }

    /**
     * 保存岗位权限
     *
     * @param idAndIds  id为岗位id，ids为设置的权限ids
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.setactions')")
    @PostMapping("/setactions")
    public CupkResponseResult saveAction(@Valid @RequestBody IdAndIdsDto idAndIds) {
        postServiceImpl.savePostActions(idAndIds.getId(), idAndIds.getIds());
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**
     * 获得岗位权限
     *
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.getactions')")
    @PostMapping("/getactions")
    public CupkResponseResult getActions(@RequestBody IdDto id) {
        List<String> actions = postServiceImpl.findActions(id.getId());
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, actions);
    }


    /**
     * 添加数据
     *
     * @param postDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.add')")
    @PostMapping
    public CupkResponseResult addSubmit(@Valid @RequestBody PostDto postDto) {
        Post post = postServiceImpl.insertPostEntity(CupkBeanUtils.copyProperties(postDto, new Post()));
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, post.getId());
    }

    /**修改数据
     * @param id
     * @param postDto
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.edit')")
    @PutMapping("/{id}")
    public CupkResponseResult editSubmit(@PathVariable String id, @Valid @RequestBody PostDto postDto) {
        postDto.setId(id);
        postServiceImpl.editPostEntity(CupkBeanUtils.copyProperties(postDto, new Post()));
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**删除数据
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.del')")
    @DeleteMapping("/{id}")
    public CupkResponseResult delSubmit(@PathVariable String id) {
        postServiceImpl.delPost(id);
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS, id);
    }

    /**
     * 批量删除数据
     *
     * @param ids 要删除的记录ID列表
     * @return 操作结果
     */
    @PreAuthorize("@cupkAction.has('post.del')")
    @DeleteMapping("/batch")
    public CupkResponseResult batchDelSubmit(@RequestBody List<String> ids) {
        for (String id : ids) {
            postServiceImpl.delPost(id);
        }
        return CupkResponseResult.getInstance(CupkResponseCode.SUCESS);
    }

    /**数据浏览
     * @param id
     * @return
     */
    @PreAuthorize("@cupkAction.has('post.info')")
    @GetMapping("/{id}")
    public CupkResponseResult info(@PathVariable String id) {
        Post post = postServiceImpl.getPostById(id);
        return new CupkResponseResult(CupkResponseCode.SUCESS, CupkBeanUtils.copyProperties(post, new PostDto()));
    }
}
